from types import SimpleNamespace

from app.db.clickhouse_db import get_clickhouse_pool, ClickHouseHTTPPool
from app.models.kkgc_model import KkgcModel
import logging

from app.services.kkgc_csv_service import kkgc_csv_service


class KKGCService:
    sql: str = "SELECT  PLATE_NO, formatDateTime(PASSING_TIME, '%Y-%m-%d %H:%M:%S')  PASSING_TIME,  SPOTTING_NO, SPOTTING_NAME FROM kkgcxx"

    def search_kkgc_list(self, kkgcModel: KkgcModel):
        clickHouseHTTPPool: ClickHouseHTTPPool = get_clickhouse_pool()
        # 业务逻辑处理
        where_conditions = ["1=1"]
        if hasattr(kkgcModel, 'vehicle_no') and kkgcModel.vehicle_no:
            where_conditions.append(f" PLATE_NO = '{kkgcModel.vehicle_no}'")

        if hasattr(kkgcModel, 'spotting_no') and kkgcModel.spotting_no:
            spotting_no_where = ",".join(f"'{item}'" for item in kkgcModel.spotting_no.split(","))
            where_conditions.append(f" SPOTTING_NO in ({spotting_no_where})")

        if hasattr(kkgcModel, 'lane_no') and kkgcModel.lane_no:
            where_conditions.append(f" LANE_NO ={kkgcModel.lane_no}")

        if hasattr(kkgcModel, 'min_run_speed') and kkgcModel.min_run_speed:
            where_conditions.append(f" RUN_SPEED >= {kkgcModel.min_run_speed}")

        if hasattr(kkgcModel, 'max_run_speed') and kkgcModel.max_run_speed:
            where_conditions.append(f" RUN_SPEED <= {kkgcModel.max_run_speed}")

        if hasattr(kkgcModel, 'passing_start_time') and kkgcModel.passing_start_time:
            where_conditions.append(f" PASSING_TIME >= '{kkgcModel.passing_start_time}'")

        if hasattr(kkgcModel, 'passing_end_time') and kkgcModel.passing_end_time:
            where_conditions.append(f" PASSING_TIME <= '{kkgcModel.passing_end_time}'")

        if hasattr(kkgcModel, 'plate_color') and kkgcModel.plate_color:
            where_conditions.append(f" PLATE_COLOR = '{kkgcModel.plate_color}'")

        if hasattr(kkgcModel, 'plate_type') and kkgcModel.plate_type:
            where_conditions.append(f" PLATE_TYPE = '{kkgcModel.plate_type}'")

        if hasattr(kkgcModel, 'vehicle_type') and kkgcModel.vehicle_type:
            where_conditions.append(f" VEHICLE_TYPE = '{kkgcModel.vehicle_type}'")

        where = "WHERE " + " AND ".join(where_conditions)

        execute_sql = f" {self.sql} {where} order by PASSING_TIME desc "
        logging.info(f"实时过车查询sql:{execute_sql}")
        return clickHouseHTTPPool.execute(execute_sql)

    def search_kkgc_csv_list(self, kkgcModel: KkgcModel):
        datas: list = self.search_kkgc_list(kkgcModel)
        if not datas:
            return datas
        return [data for data in datas if
                kkgc_csv_service.check_vehicle_in_csv(kkgcModel.compare_package, data['PLATE_NO'])]

    def search_kkgc_csv_list_result(self, body: list):
        result: list = list()
        i: int = 0
        for body_dict in body:
            kkgcModel: KkgcModel = SimpleNamespace(**body_dict)
            datas: list = None
            i += 1
            if hasattr(kkgcModel, 'compare_package') and kkgcModel.compare_package:
                datas = self.search_kkgc_csv_list(kkgcModel)
            else:
                datas = self.search_kkgc_list(kkgcModel)
            if datas:
                for info in datas:
                    result.append(
                        f"{kkgcModel.ruler_name} {info['PLATE_NO']} ， 经过时刻：{info['PASSING_TIME']} ， 卡口编号：{info['SPOTTING_NO']}")
        return result


kkgc_service: KKGCService = KKGCService()
